<?php
App::import('Controller', 'Messages');
/**
 *
 */
class PostsController extends AppController {

	//public $uses = array('Group', 'User');

	public $helpers = array('Html', 'Form');

	public function isAuthorized($user) {
		return true;
	}

	public function index() {
		$result = $this -> Post -> query('SELECT name FROM faculties WHERE id_faculty = ' . (int)$this -> Auth -> user('id_faculty'));
		$user_logged = array('id' => $this -> Auth -> user('id_user'), 'id_faculty' => $this -> Auth -> user('id_faculty'), 'name_faculty' => $result[0]['faculties']['name']);
		$this -> set('user_logged', $user_logged);
		$result = $this -> Post -> query("SELECT Post.id_post, Post.text, Post.created, Faculty.id_faculty, Faculty.name, User.id_user, User.name, User.surname FROM posts AS Post LEFT JOIN faculties AS Faculty ON Post.id_faculty=Faculty.id_faculty LEFT JOIN users as User ON Post.id_user=User.id_user ORDER BY Post.created DESC;");
		$this -> set('posts', $result);
		$this -> set('faculties', $this -> Post -> Faculty -> find('all'));

		$result = $this -> Post -> query('SELECT notified_file FROM users AS User WHERE id_user=' . $this -> Auth -> user('id_user'));
		$new_files_personal = array();
		$new_files_personal = $result[0]['User']['notified_file'];
		if ($new_files_personal > 0) {
			$this -> Session -> setFlash('You have ' . $new_files_personal . ' new ' . ($new_files_personal > 1 ? 'files' : 'file') . ' shared with you');
			$this -> Post -> query('UPDATE users SET notified_file=0 WHERE id_user=' . $this -> Auth -> user('id_user'));
		}

		$result = $this -> Post -> query('SELECT notified_group FROM users AS User WHERE id_user=' . $this -> Auth -> user('id_user'));
		$new_files_group = array();
		$new_files_group = $result[0]['User']['notified_group'];
		if ($new_files_group > 0) {
			$this -> Session -> setFlash('You have ' . $new_files_group . ' new ' . ($new_files_group > 1 ? 'files' : 'file') . ' shared in your groups');
			$this -> Post -> query('UPDATE users SET notified_group=0 WHERE id_user=' . $this -> Auth -> user('id_user'));
		}

		$allUsers = $this -> Post -> query("SELECT * FROM cake_sessions;");
		$users_logged_in = array();
		$list_of_logged_id = array();
		foreach ($allUsers as $user) {
			$user = MessagesController::unserializesession($user['cake_sessions']['data']);
			//Only distint and except yourself
			if (isset($user['Auth']['User']) && !in_array($user['Auth']['User']['id_user'], $list_of_logged_id) && $user['Auth']['User']['id_user'] != $this -> Auth -> user('id_user')) {
				$list_of_logged_id[] = $user['Auth']['User']['id_user'];
				$users_logged_in[] = $user['Auth']['User'];
			}
		}
		$this -> set('users', $users_logged_in);
	}

	public function add() {
		$data = $this -> request -> data;
		pr($data);
		$save = array('Post' => array('text' => $data['Post']['text'], 'created' => date("Y-m-d H:i:s"), 'id_user' => $this -> Auth -> user('id_user'), 'id_faculty' => $data['Post']['scope'] == '0' ? '' : $data['Post']['scope']));

		if ($this -> Post -> save($save)) {
			$this -> Session -> setFlash('Post has been created successfully.');
		} else {
			$this -> Session -> setFlash('Unable to create new post.');
		}
		$this -> redirect(array('action' => 'index'));
	}

}
